package com.example.teamtribe.hello;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.common.model.UserModel;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.Utility;

public class HelloPresenter implements HelloContract.Presenter {

    private HelloContract.View view;
    private HelloModel model;

    public HelloPresenter(HelloContract.View view) {
        this.view = view;
        model = new HelloModel();
    }

    @Override
    public void getUser(int id) {
        view.showProgress();
        UserModel.getInstance().getUser(Utility.getToken(), id, new FileModel.FileBack() {
            @Override
            public void loadSuccess(String fileName) {
                view.next();
            }

            @Override
            public void success(@Nullable Bundle bundle) {
                Utility.setUser((User) bundle.getParcelable("user"));
            }

            @Override
            public void failure(String meg) {
                updateFailureView(meg);
            }
        });
    }

    @Override
    public void login(final String username, String psw) {
        view.showProgress();
        model.login(username, psw, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                Utility.setToken(bundle.getString("TeamTribeToken"));
                int id = bundle.getInt("id");
                //LitePal.use(LitePalDB.fromDefault("TeamTribe" + id));
                getUser(id);
            }

            @Override
            public void failure(String meg) {
                if (meg.equals(HttpUtil.getCodeMeg(2))) {
                    updateFailureView(meg);
                } else {    //只有密码错误才更新验证码
                    view.hideProgress();
                    view.showToast(meg);
                }
            }
        });
    }

    @Override
    public void sendCode(String phone) {
        model.sendCode(phone, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {

            }

            @Override
            public void failure(String meg) {
                updateFailureView(meg);
            }
        });
    }

    @Override
    public void register(final String username, final String psw1, String psw2, final String phone, String email, String code) {
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(psw1) || TextUtils.isEmpty(psw2)
                || phone.length() != 11 || TextUtils.isEmpty(code)) {
            view.showToast("请填写完整信息");
            return;
        }
        if (!psw1.equals(psw2)) {
            //view.showToast("两次输入密码不同");
            //return;
        }
        if (!TextUtils.isEmpty(email) && !email.contains("@")) {
            view.showToast("邮箱地址不合法");
            return;
        }

        view.showProgress();
        model.register(username, psw1, phone, code, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                login(phone, psw1);
            }

            @Override
            public void failure(String meg) {
                updateFailureView(meg);
            }
        });
    }

    private void updateFailureView(String meg) {
        view.hideProgress();
        view.updateCode();
        view.showToast(meg);
    }
}
